package com.pansoft.openplanet.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.Account;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.efounder.frame.utils.Constants.CHAT_USER_ID;

/**
 * Created by YQS on 2018/3/27.
 */

public class ChildAccountDao {

    private Context context;
    private SQLiteDatabase db;
    private static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public ChildAccountDao(Context context) {
        this.context = context;
        db = WalletDbHelper.getInstance(context).getDb();
    }

    public void closeDb() {
        if (db != null) {
            db.close();
        }
    }

    public long insertOrReplace(Account account) {
        ContentValues values = getContentValue(account);
        return this.db.insertWithOnConflict("account1", null, values,
                SQLiteDatabase.CONFLICT_REPLACE);
    }

    public long update(Account account) {
        ContentValues values = getContentValue(account);
        return this.db.update("account1", values,
                "address=? and im_userid =?", new String[]{values.getAsString("address"), values.getAsString("im_userid")});
    }


    public long delete(String address) {
        return this.db.delete("account1", "address =? and im_userid =?",
                new String[]{address.toLowerCase(), EnvironmentVariable.getProperty(CHAT_USER_ID, "")});
    }

    public long deleteByCurentImuserId() {
        return this.db.delete("account1", " im_userid =?",
                new String[]{EnvironmentVariable.getProperty(CHAT_USER_ID, "")});
    }

    public Account getAccount(String address) {
        String sql = "select * from account1 where address =? and im_userid=?";
        List<Account> list = query(sql, new String[]{address.toLowerCase(),
                EnvironmentVariable.getProperty(CHAT_USER_ID, "")});
        return list.size() == 0 ? null : list.get(0);
    }

    public List<Account> getAllAccount() {
        String sql = "select * from account1 where im_userid =?";
        List<Account> list = query(sql, new String[]{EnvironmentVariable.getProperty(CHAT_USER_ID, "")});
        return list;
    }

    private List<Account> query(String sql) {
        return query(sql, null);
    }

    private List<Account> query(String sql, String[] selectionArgs) {
        List<Account> list = new ArrayList<Account>();
        Cursor cursor = this.db.rawQuery(sql, selectionArgs);
        while (cursor.moveToNext()) {
            Account account = new Account();
            account.setAddress(cursor.getString(cursor.getColumnIndex("address")));
            String name = cursor.getString(cursor.getColumnIndex("account_name"));
            String avatar = cursor.getString(cursor.getColumnIndex("account_avatar"));
            String time = cursor.getString(cursor.getColumnIndex("create_time"));
            String filename = cursor.getString(cursor.getColumnIndex("account_filename"));
            String imUserId = cursor.getString(cursor.getColumnIndex("im_userid"));
            if (name == null || "".equals(name)) {
                account.setAcccountName(ResStringUtil.getString(R.string.open_planet_asset_child_account_account));
            } else {
                account.setAcccountName(name);
            }
            if (avatar == null || "".equals(avatar)) {
                account.setAccountAvatar("");
            } else {
                account.setAccountAvatar(avatar);
            }
            if (time == null || "".equals(time)) {
                account.setCreateTime(null);
            }
            account.setFileName(filename);
            account.setImUserId(imUserId);

            list.add(account);
        }
        cursor.close();
        return list;
    }

    private ContentValues getContentValue(Account account) {
        ContentValues values = new ContentValues();
        values.put("address", account.getAddress().toLowerCase());
        values.put("account_avatar", account.getAccountAvatar());
        values.put("account_filename", account.getFileName());

        values.put("create_time", sf.format(new Date()));
        if (account.getAcccountName() == null || "".equals(account.getAcccountName())) {
            values.put("account_name", ResStringUtil.getString(R.string.open_planet_asset_child_account_account));
        } else {
            values.put("account_name", account.getAcccountName());
        }

        values.put("im_userid", account.getImUserId() == null ? EnvironmentVariable.getProperty(CHAT_USER_ID) : account.getImUserId());
        return values;
    }
}




